package com.tbvtc.agriculturalcreditchainserver2025.controller.user;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tbvtc.agriculturalcreditchainserver2025.dto.User.FindPasswordDto;
import com.tbvtc.agriculturalcreditchainserver2025.dto.User.LoginByCaptchaDto;
import com.tbvtc.agriculturalcreditchainserver2025.dto.User.LoginByPasswordDto;
import com.tbvtc.agriculturalcreditchainserver2025.dto.User.UpdatePasswordDto;
import com.tbvtc.agriculturalcreditchainserver2025.entity.FarmlandInfo;
import com.tbvtc.agriculturalcreditchainserver2025.entity.User;
import com.tbvtc.agriculturalcreditchainserver2025.exception.FoodException;
import com.tbvtc.agriculturalcreditchainserver2025.service.IFarmlandInfoService;
import com.tbvtc.agriculturalcreditchainserver2025.service.IUserService;
import com.tbvtc.agriculturalcreditchainserver2025.service.IpfsService;
import com.tbvtc.agriculturalcreditchainserver2025.service.SmsService;
import com.tbvtc.agriculturalcreditchainserver2025.service.impl.AuthServiceImpl;
import com.tbvtc.agriculturalcreditchainserver2025.utils.RUtils;
import com.tbvtc.agriculturalcreditchainserver2025.utils.Result;
import com.tbvtc.agriculturalcreditchainserver2025.utils.ResultCodeEnum;
import com.tbvtc.agriculturalcreditchainserver2025.vo.CaptchaVo;
import com.tbvtc.agriculturalcreditchainserver2025.vo.LoginDto;
import com.tbvtc.agriculturalcreditchainserver2025.vo.UserVo;
import com.tbvtc.agriculturalcreditchainserver2025.vo.farmerVo.loginVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Base64;
import java.util.List;


@Slf4j
@CrossOrigin
@RestController("UserAuthController")
@RequestMapping("/user")
@Tag(name = "App端用户认证相关接口")
public class AuthController {

    @Autowired
    private AuthServiceImpl ServiceImpl;
    @Autowired
    private IUserService userService;
    @Autowired
    private SmsService smsService;
    @Autowired
    private AuthServiceImpl authService;
    @Resource
    private IFarmlandInfoService farmlandInfoService;

    /**
     * 用户密码登录
     *
     * @param loginDto
     * @return
     */
    @PostMapping("/loginByPassword")
    @Operation(summary = "用户密码登录")
    public Result loginByPassword(@RequestBody LoginByPasswordDto loginDto) {
        log.info("用户密码登录");
        loginVo loginVo = userService.loginByPassword(loginDto);
        return RUtils.success(loginVo);
    }

    /**
     * 用户验证码登录
     *
     * @param loginDto
     * @return
     */
    @PostMapping("/loginByCaptcha")
    @Operation(summary = "用户验证码登录")
    public Result loginByCaptcha(@RequestBody LoginByCaptchaDto loginDto) {
        log.info("用户验证码登录");
        return RUtils.success(userService.loginByCaptcha(loginDto));
    }

    /**
     * 发送短信
     *
     * @param phone
     * @return
     */
    @GetMapping("/getCode")
    @Operation(summary = "发送短信")
    public Result getCode(@RequestParam String phone) {
        smsService.sendPestCode(phone, "五号地");
        log.info("短信发送成功");
        return RUtils.success();
    }

    /**
     * 注册
     */
    @PostMapping("/registered")
    @Operation(summary = "注册账号")
    public Result registered(@RequestBody UserVo userVo) {
        authService.registerFarmer(userVo);
        return RUtils.success();
    }

    @Operation(summary = "修改密码")
    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestBody UpdatePasswordDto updatePassword) {
        log.info("修改密码");
        return userService.updatePassword(updatePassword);
    }


    @Operation(summary = "获取token时间")
    @GetMapping("/getTokenTime")
    public Result getTokenTime(String token) {
        log.info("获取token时间");

        return userService.getTokenTime(token);
    }

    @Operation(summary = "获取验证码")
    @GetMapping("/login/captcha")
    public Result<CaptchaVo> captcha(String phone) {
        authService.getCode(phone);
        return RUtils.success();
    }

    @Operation(summary = "找回密码")
    @PostMapping("/findPassword")
    public Result findPassword(@RequestBody FindPasswordDto findPasswordDto) {
        log.info("找回密码");
        authService.findPassword(findPasswordDto.getPhone(), findPasswordDto.getNewPassword(), findPasswordDto.getCode());
        return RUtils.success();
    }


}
